#include"Stack.h"

void StackInit(Stack* ps) {
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}
void StackPush(Stack* ps, DataType x) {
	assert(ps);
	if (ps->capacity == ps->size) {
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		DataType* tmp = (DataType*)realloc(ps->a, sizeof(DataType) * newCapacity);
		if (tmp == NULL) {
			perror("realloc fail");
			exit(1);
		}
		ps->a = tmp;
		ps->capacity = newCapacity;
	}
	ps->a[ps->size++] = x;
}
void StackPop(Stack* ps) {
	assert(ps->size);
	ps->size--;
}
int StackTop(Stack* ps) {
	assert(ps->size);
	return ps->a[ps->size - 1];
}
bool StackEmpty(Stack* ps) {
	return ps->size == 0;
}
void StackDestroy(Stack* ps) {
	if(ps->a!=NULL)
		free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}

void StackPrint(Stack st) {
	for (int i = 0; i < st.size; i++) {
		printf("%d ", st.a[i]);
	}
	printf("\n");
}